#!/bin/sh
#
# description: Starts and stops the RSS Ex
#
mos=/usr/local/etc/mos/rssex
momenu=/usr/local/etc/moMenu.rss

case "$1" in
  start)
	echo "Starting RSS Ex..."
	cp -Rfp $mos/web/* /tmp/www/
	[ ! -e /usr/local/etc/rssex ] && ln -s $mos/rss/ /usr/local/etc/rssex
	;;
  stop)
	echo "Stopping RSS Ex..."
	rm -f /usr/local/etc/rssex
	;;

  status)
	if [ -e /usr/local/etc/rssex ] ; then
	  echo "RSS Ex is running"
	else
	  echo "RSS Ex is stopped"
	fi
	;;

  enable)
	echo "Enabling RSS Ex..."

	# add lib to system
	if [ -z $( cat /etc/ld.so.conf | grep rssex/lib ) ] ; then
	  echo "$mos/lib" >> /usr/local/etc/ld.so.conf
	  $mos/ldconfig -C /usr/local/etc/ld.so.cache 2>/dev/null
	fi

	# coping files
	cp -fp  $mos/xspf.conf /usr/local/etc/
	cp -fp  $mos/xspf.scan /usr/local/etc/
	cp -fp  $mos/index.m3u /usr/local/etc/

	if [ ! -d /usr/local/etc/playlist ] ; then
	  mkdir /usr/local/etc/playlist
	  cp -fp $mos/playlist/* /usr/local/etc/playlist/
	fi

	# adding to RSS
	if [ ! -f $momenu ] ; then
	  cp /usr/local/etc/mos/core/rss/moMenu.rss /usr/local/etc/
	fi

	if [ -z "$( cat $momenu | grep etc/rssex )" ] ; then
	  sed -i '
/<title>moServices<\/title>/i\
		<title>YouTube videos</title>\
		<link>rss_file://../etc/rssex/youtube/yt_index.rss</link>\
		<media:thumbnail url="/usr/local/etc/rssex/image/menu_youtube.png" width="120" height="90" />\
	</item>\
	<item>\
		<title>Shoutcast Radio</title>\
		<link>rss_file://../etc/rssex/shoutcast/radio_shoutcast.rss</link>\
  		<media:thumbnail url="/usr/local/etc/rssex/image/menu_shoutcast_radio.png" width="120" height="90" />\
	</item>\
	<item>\
		<title>XSPF Playlists</title>\
		<link>rss_file://../etc/rssex/xspf/xspf.rss</link>\
		<media:thumbnail url="/usr/local/etc/rssex/image/menu_xspf.png" width="120" height="90" />\
	</item>\
	<item>\
		<title>XSPF Browser</title>\
		<link>rss_file://../etc/rssex/xspf/xspfBrowser.rss</link>\
		<media:thumbnail url="/usr/local/etc/rssex/image/menu_xspf_browser.png" width="120" height="90" />\
	</item>\
	<item>\
		<title>Media Stream Renderer</title>\
		<link>rss_file://../etc/rssex/xspf/streamRenderer.rss</link>\
		<media:thumbnail url="/usr/local/etc/rssex/image/menu_msr.png" width="120" height="90" />\
	</item>\
	<item>
' $momenu
	fi
	;;

  disable)
	echo "Disabling RSS Ex..."

	# Remove from RSS
	sed -i '
/<title>Shoutcast Radio<\/title>/{N;N;N;N;d}
/<title>YouTube videos<\/title>/{N;N;N;N;d}
/<title>XSPF Playlists<\/title>/{N;N;N;N;d}
/<title>XSPF Browser<\/title>/{N;N;N;N;d}
/<title>Media Stream Renderer<\/title>/{N;N;N;N;d}
' $momenu

	# Remove files
	rm -f /usr/local/etc/rssex
	rm -f usr/local/etc/index.m3u
	rm -f /usr/local/etc/xspf.scan
	rm -f /usr/local/etc/xspf.conf

	# remove lib from system
	if [ ! -z $( cat /etc/ld.so.conf | grep rssex/lib ) ] ; then
	  sed -i '/rssex/d' /usr/local/etc/ld.so.conf
	  $mos/ldconfig -C /usr/local/etc/ld.so.cache 2>/dev/null
	fi
	;;

  *)
	echo "Usage: $0 {start|stop|status|enable|disable}"
esac

exit $?
